package ru.averta.notes.service.search;

import ru.averta.notes.other.UtilsWords;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * <code>SubjectCriteriaSimpleBean</code>
 *
 * Хранит условия фильтрации списка subject'ов
 * Используется в обычном случае - заполнили, нашли
 *
 * @author: fedor.belov (fedor.belov@mail.ru)
 * Date: 06.06.11 21:56
 */
public class SubjectCriteriaSimpleBean implements SubjectCriteriaBean {

    private boolean isFilterFavorite = false;
    private String fulltext = "";
    private List<String> fulltextWords = null;
    private Collection<String> filterTags = null;
    private SubjectFilterTarget target = SubjectFilterTarget.All;
    private Long targetFolderId = null;

    public SubjectCriteriaSimpleBean filterByFavorite() {
        isFilterFavorite = true;
        return this;
    }

    public SubjectCriteriaSimpleBean filterByFulltext(String fulltext) {
        this.fulltext = fulltext;
        this.fulltextWords = Arrays.asList(UtilsWords.splitToWords(fulltext));
        return this;
    }

    public SubjectCriteriaSimpleBean filterByTags(String... tag) {
        if (tag != null) {
            setFilterTags(Arrays.asList(tag));
        }
        return this;
    }

    public SubjectCriteriaSimpleBean filterByTags(Collection<String> tags) {
        setFilterTags(tags);
        return this;
    }

    private void setFilterTags(Collection<String> tags) {
        Collection<String> target = new ArrayList<String>(tags.size());

        for (String tag : tags) {
            target.add(tag.toLowerCase());
        }
        filterTags = target;
    }

    public SubjectCriteriaSimpleBean setTargetAll() {
        target = SubjectFilterTarget.All;
        return this;
    }

    public SubjectCriteriaSimpleBean setTargetInbox() {
        target = SubjectFilterTarget.Inbox;
        return this;
    }

    public SubjectCriteriaSimpleBean setTargetArchive() {
        target = SubjectFilterTarget.Archive;
        return this;
    }

    @Override
    public String getFullText() {
        return fulltext;
    }

    public List<String> getFullTextWords() {
        return fulltextWords;
    }

    @Override
    public SubjectFilterTarget getTarget() {
        return target;
    }

    @Override
    public Collection<String> getFilterTags() {
        return filterTags;
    }

    @Override
    public boolean isFilterFavorite() {
        return isFilterFavorite;
    }

}
